package com.hooyh.controller;

import com.hooyh.domian.User;
import com.hooyh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by hooyh on 2017/3/28.
 */
@RestController
@RequestMapping("user")
public class UserController {
    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    /**
     * 新增
     * @param user
     * @return
     */
    @PostMapping
    public User create(@RequestBody User user){
        return userService.save(user);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public boolean delete(@PathVariable Long id){
        return userService.delete(id);
    }

    /**
     * 修改
     * @param id
     * @param user
     * @return
     */
    @PutMapping("{id}")
    public User update(@PathVariable Long id,@RequestBody User user){
        user.setId(id);
        return userService.save(user);
    }

    /**
     * 查询
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public User find(@PathVariable Long id){
        return userService.find(id);
    }
}
